pub mod icc_deal;

use std::error::Error;
use std::fs::File;
use std::io::Read;

#[derive(Debug)]
pub struct IccParam {
    pub ip: String,
    pub ue_port: u32,
    pub up_port: u32,
    pub dstate: u8,
    pub ustate: u8,
    pub vstate: u8,
    pub ueupver: String,
}

impl IccParam {
    pub fn parse_and_build(file_path: &str) -> Result<IccParam, Box<dyn Error>> {
        let mut file = File::open(file_path)?;
        let mut contents = String::new();
        file.read_to_string(&mut contents)?;

        let json_value: serde_json::Value = serde_json::from_str(&contents)?;

        // 手动解析 JSON 字段
        let ip = json_value["icc_ip"].as_str().ok_or("icc_ip not found or invalid")?.to_string();
        let ue_port = json_value["icc_ue_port"].as_u64().ok_or("icc_ue_port not found or invalid")? as u32;
        let up_port = json_value["icc_up_port"].as_u64().ok_or("icc_up_port not found or invalid")? as u32;
        let dstate = json_value["dstate"].as_u64().ok_or("dstate not found or invalid")? as u8;
        let ustate = json_value["ustate"].as_u64().ok_or("ustate not found or invalid")? as u8;
        let vstate = json_value["vstate"].as_u64().ok_or("vstate not found or invalid")? as u8;
        let ueupver = json_value["ueup_version"].as_str().ok_or("ueupver not found or invalid")?.to_string();

        Ok(IccParam { 
            ip, 
            ue_port, 
            up_port,
            dstate,
            ustate,
            vstate,
            ueupver
        })
    }

    pub fn clone(&self) -> IccParam {
        IccParam {
            ip: self.ip.clone(),
            ue_port: self.ue_port,
            up_port: self.up_port,
            dstate: self.dstate,
            ustate: self.ustate,
            vstate: self.vstate,
            ueupver: self.ueupver.clone(),
        }
    }
}


